package com.grmasa.soundtoggle;


public class MyLog {

    // 控制日志是否输出的开关，true表示输出，false表示不输出，方便在正式发布时关闭所有日志输出
    public static boolean DEBUG = true;
    // 控制是否增加明显的字符，方便查找问题
    public static boolean LookMe=true;

    // 定义日志的级别，这里简单模拟常用的几种
    public static final int VERBOSE = 1;
    public static final int DEBUG_LEVEL = 2;
    public static final int INFO = 3;
    public static final int WARN = 4;
    public static final int ERROR = 5;
    // 当前设置的日志级别，只有小于等于该级别的日志才会输出
    public static int currentLevel = DEBUG_LEVEL;

    // 类名的缓存，避免每次获取类名的反射开销
    private static final ThreadLocal<String> classNames = new ThreadLocal<>();

    // 辅助方法，获取调用者的类名
    private static String getClassName() {
        String className = classNames.get();
        if (className == null) {
            // 通过异常栈信息获取调用者的类名
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            boolean found = false;
            for (StackTraceElement element : stackTrace) {
                String cn = element.getClassName();
                if (cn.equals(MyLog.class.getName())) {
                    found = true;
                    continue;
                }
                if (found) {
                    className = cn.substring(cn.lastIndexOf('.') + 1);
                    classNames.set(className);
                    break;
                }
            }
        }
        return className;
    }

    private static void look(){
        if (LookMe){
            android.util.Log.v("","\n\n\n██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※██※\n");
        }
    }
    // 输出VERBOSE级别的日志
    public static void v(String msg) {
        if (DEBUG && VERBOSE <= currentLevel) {
            look();
            android.util.Log.v(getClassName(), msg);
        }
    }

    // 输出DEBUG级别的日志
    public static void d(String msg) {
        if (DEBUG && DEBUG_LEVEL <= currentLevel) {
            look();
//            android.util.Log.d(getClassName(), msg);
            if (msg.length() > 4000) {
                int chunkCount = msg.length() / 4000;
                for (int i = 0; i <= chunkCount; i++) {
                    int max = 4000 * (i + 1);
                    if (max >= msg.length()) {
                        android.util.Log.d(getClassName(), msg.substring(4000 * i));
                    } else {
                        android.util.Log.d(getClassName(), msg.substring(4000 * i, max));
                    }
                }
            } else {
                android.util.Log.d(getClassName(), msg);
            }
        }
    }

    // 输出INFO级别的日志
    public static void i(String msg) {
        if (DEBUG && INFO <= currentLevel) {
            look();
            android.util.Log.i(getClassName(), msg);
        }
    }

    // 输出WARN级别的日志
    public static void w(String msg) {
        if (DEBUG && WARN <= currentLevel) {
            look();
            android.util.Log.w(getClassName(), msg);
        }
    }

    // 输出ERROR级别的日志
    public static void e(String msg) {
        if (DEBUG && ERROR <= currentLevel) {
            look();
            android.util.Log.e(getClassName(), msg);
        }
    }
}